﻿using System.Collections.Generic;
using System.Reflection;

namespace UtilZ.Dotnet.Ex.Communication.Net.Web
{
    /// <summary>
    /// 动态api接口
    /// </summary>
    public interface IDynamicApi
    {
        /// <summary>
        /// 获取api函数信息
        /// </summary>
        /// <returns></returns>
        IEnumerable<DynamicApiInfo> GetWebApiInfos();
    }

    /// <summary>
    /// 动态api信息
    /// </summary>
    public class DynamicApiInfo
    {
        /// <summary>
        /// 路由模板(eg:[Route("api/v1/[controller]")])
        /// </summary>
        public string Template { get; private set; }

        /// <summary>
        /// http请求类型
        /// </summary>
        public string HttpRequestMethod { get; private set; }

        /// <summary>
        /// 自定义数据转换器
        /// </summary>
        public IHttpDataConverter Converter { get; set; }

        /// <summary>
        /// 调用方法信息
        /// </summary>
        public MethodInfo ApiMethodInfo { get; private set; } = null;

        /// <summary>
        /// 调用方法名称
        /// </summary>
        public string ApiMethodName { get; private set; } = null;



        private DynamicApiInfo(string httpRequestMethod, string template)
        {
            this.HttpRequestMethod = httpRequestMethod;
            this.Template = template;
        }

        /// <summary>
        /// 构造函数初始化
        /// </summary>
        /// <param name="httpRequestMethod">http请求类型</param>
        /// <param name="apiMethodName">调用方法名</param>
        /// <param name="template">路由模板(eg:[Route("api/v1/[controller]")])</param>
        public DynamicApiInfo(string httpRequestMethod, string apiMethodName, string template)
            : this(httpRequestMethod, template)
        {
            this.ApiMethodName = apiMethodName;
        }

        /// <summary>
        /// 构造函数初始化
        /// </summary>
        /// <param name="httpRequestMethod">http请求类型</param>
        /// <param name="apiMethodInfo">调用方法信息</param>
        /// <param name="template">路由模板(eg:[Route("api/v1/[controller]")])</param>
        public DynamicApiInfo(string httpRequestMethod, MethodInfo apiMethodInfo, string template)
            : this(httpRequestMethod, template)
        {
            this.ApiMethodInfo = apiMethodInfo;
        }


    }
}
